'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-04-14 21:07:53
'''
import sys
import numpy as np
import json
class DealData():
    def __init__(self, time_out):
        self.time = []
        self.uuid_time = {}
        self.time_out = time_out
    
    def read_file(self, filename):
        with open(filename) as f:
            for line in f:
                line = line.split(' ')
                if len(line)<5:
                    continue
                if line[-5] == 'whole' and line[-4] == 'process' and line[-3] == 'takes':
                    tmp = float(line[-2])
                    self.time.append(tmp)
                    if tmp > self.time_out:
                        # print(line)
                        for i, v in enumerate(line):
                            if v == 'uuid':
                                tmpp = line[i+2][:-1]
                                self.uuid_time[tmpp] = tmp
    
    def sort(self):
        tmp = np.array(self.time)
        print('50%', np.percentile(tmp,50))
        print('70%', np.percentile(tmp,70))
        print('90%', np.percentile(tmp,90))
        print('98%', np.percentile(tmp,98))
        print('99%', np.percentile(tmp,99))
        print('99.5%', np.percentile(tmp,99.5))
        print('99.7%', np.percentile(tmp,99.7))
        print('99.9%', np.percentile(tmp,99.9))
        print('100%', np.percentile(tmp,100))
        # cnt = 0
        # for i in tmp:
        #     cnt += i>0.28

        # print('the time out rage is', cnt/len(tmp))
        # print('the number of thread is ', 20)
        # print('the qps is ', len(self.time)/(60*60*24))
        print('the time_out size is ', len(self.uuid_time))
        print('the rate of the outime is ', len(self.uuid_time)/len(self.time))
        print('all of the reqs is ', len(self.time))



class DetDealData(DealData):
    def __init__(self, time_out):
        DealData.__init__(self, time_out)
        self.store_uuid = {}
    
    def read_file(self, filename):
        with open(filename) as f:
            for line in f:
                line = line.split(' ')
                if len(line)<8:
                    continue
                if line[-8] == 'embedding' and line[-7] == 'size':
                    self.store_uuid[line[-10][:-1]] = {'size':int(line[-5])}
                if line[-5] == 'whole' and line[-4] == 'process' and line[-3] == 'takes':
                    tmp = float(line[-2])
                    self.time.append(tmp)
                    for i, v in enumerate(line):
                        if v == 'uuid':
                            tmpp = line[i+2][:-1]
                    if tmpp not in self.store_uuid:
                        self.store_uuid[tmpp] = {}
                    self.store_uuid[tmpp]['time'] = tmp
                    if tmp > self.time_out:
                        for i, v in enumerate(line):
                            if v == 'uuid':
                                tmpp = line[i+2][:-1]
                                self.uuid_time[tmpp] = tmp
                            if v[:3] == 'uid':
                                self.store_uuid[tmpp]['uid'] = int(v[4:-1])
                            if v[:5] == 'jepId':
                                self.store_uuid[tmpp]['jepid'] = int(v[6:-1])
                            if v[:4] == 'city':
                                self.store_uuid[tmpp]['city'] = int(v[5:-1])
                            if v[:8] == 'position':
                                self.store_uuid[tmpp]['position'] = int(v[9:-1])
                    else:
                        del(self.store_uuid[tmpp])


    def deal_uuidmap(self, write_file):
        # import pandas as pd
        s = json.dumps(self.store_uuid)
        # s.replace('},{', '}\n{')
        with open('store_uuid', 'w') as f:
            f.write(s)
        

if __name__ == "__main__":
    deal_d = DetDealData(0.14)
    filename = sys.argv[1]
    write_file = 'store_uuid_pt'
    deal_d.read_file(filename)
    # deal_d.deal_uuidmap(write_file)
    deal_d.sort()

